package com.szakul.pdf.model;


import java.util.List;

import org.apache.log4j.Logger;

import com.szakul.pdf.viewer.dao.beans.Leaf;
import com.szakul.pdf.viewer.dao.beans.Node;
import com.szakul.pdf.viewer.dao.beans.TreeDao;

public class TreeModelDaoImpl implements TreeModelDao{

    /**
     * log4j logger.
     */
    private static final Logger LOGGER = Logger.getLogger(TreeModelDaoImpl.class);
	
	private TreeDao treeDao;

	public TreeModelDaoImpl() {
		super();
	}
	
	public TreeModelDaoImpl(TreeDao treeDao) {
		this.treeDao = treeDao;
		LOGGER.debug("TreeModelDao bean created " + treeDao);
	}

	/**
	 * @return the treeDao
	 */
	public TreeDao getTreeDao() {
		return treeDao;
	}

	/**
	 * @param treeDao the treeDao to set
	 */
	public void setTreeDao(TreeDao treeDao) {
		this.treeDao = treeDao;
	}
	
	
	public void createEmptyTree() {
		
		final int nodeCount = getTreeDao().getNodesCount();
		
		LOGGER.debug("Nodes in system: " + nodeCount);
		
		if(nodeCount == 0) {
			
			Node node = new Node("First");
			Leaf leaf = new Leaf("First book");
			
			int nodeId = getTreeDao().createNode(node);
			LOGGER.debug("First node created with id " + nodeId);
			leaf.setNode(node);
			int leafId = getTreeDao().createLeaf(leaf);
			LOGGER.debug("First leaf created with id " + leafId);
			
			node.getLeaf().add(leaf);
			getTreeDao().updateNode(node);
		}
		
		
	}

	public List<Node> getAllNodes() {
		
		return getTreeDao().getAllNodes();
	}

	public List<Leaf> getNodeLeafs(Node node) {
		
		return getTreeDao().getNodeLeafs(node);
	}

	public void updateLeaf(Leaf leaf) {
		
		getTreeDao().updateLeaf(leaf);
	}

	public void createLeaf(Leaf leaf) {
		int id = getTreeDao().createLeaf(leaf);
		LOGGER.debug("Leaf created with id " + id);
		
	}

	public void createNode(Node node) {
		int id = getTreeDao().createNode(node);
		LOGGER.debug("Node created with id " + id);
		
	}

	public void updateNode(Node node) {
		getTreeDao().updateNode(node);
		
	}

	public void deleteLeaf(Leaf leaf) {
		
		getTreeDao().deleteLeaf(leaf);
	}

	public void deleteNode(Node node) {
		
		getTreeDao().deleteNode(node);
	}
	
	
	
}
